/**
 * 测试
 */
package jkcp;

import io.netty.buffer.ByteBuf;
import org.beykery.jkcp.KcpOnUdp;
import org.beykery.jkcp.KcpServer;
import test.PerformanceAnalyze;

import java.nio.charset.StandardCharsets;

/**
 * @author beykery
 */
public class TestServer extends KcpServer {

    static PerformanceAnalyze performanceAnalyze = new PerformanceAnalyze();

    public TestServer(int port, int workerSize) {
        super(port, workerSize);
    }

    @Override
    public void handleReceive(ByteBuf bb, KcpOnUdp kcp) {
        performanceAnalyze.meter();

        String content = bb.toString(StandardCharsets.UTF_8);

        System.out.println("msg:" + content + " kcp--> " + kcp);

        // kcp.send(bb);//echo

        performanceAnalyze.delay(System.nanoTime() - Long.parseLong(content));
    }

    @Override
    public void handleException(Throwable ex, KcpOnUdp kcp) {
        System.out.println(ex);
    }

    @Override
    public void handleClose(KcpOnUdp kcp) {
        System.out.println("客户端离开:" + kcp);
        System.out.println("waitSnd:" + kcp.getKcp().waitSnd());
    }

}
